﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Twi.COMMON.Core
{
    /*
     * CopyRight:©2012 Twilight软件开发工作室版权所有
     * Author:xuzhihong
     * Create Date: 2012-07-25
     * Description: 
     */
    public class TwiDictionaryHelper<T>
    {
        /// <summary>
        /// 获取字典的键以separator进行隔开
        /// </summary>
        /// <param name="dict">字典</param>
        /// <param name="separator">分隔符</param>
        /// <returns></returns>
        public static string GetDictionaryKeys(Dictionary<T,object> dict,string separator = ",")
        {
            string strKeys = "";
            foreach (T dy in dict.Keys)
            {
                strKeys += separator + dy;
            }
            strKeys = strKeys.Substring(separator.Length);            
            return strKeys;
        }
        public static string GetDictionaryKeys(Dictionary<string, T> dict, string separator = ",")
        {
            string strKeys = "";
            foreach (string dy in dict.Keys)
            {
                strKeys += separator + dy;
            }
            strKeys = strKeys.Substring(separator.Length);
            return strKeys;
        }


        /// <summary>
        /// 合并两个字典
        /// </summary>
        /// <param name="dict1"></param>
        /// <param name="dict2"></param>
        /// <returns></returns>
        public static Dictionary<T, object> MergeTwoDicts(Dictionary<T, object> dict1, Dictionary<T, object> dict2)
        {
            var dicts = new Dictionary<T, object>[]{ dict1,dict2};
            return MergeDictionaries(dicts);
        }

        /// <summary>
        /// 将多个字典合成为一个
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dicts"></param>
        /// <returns></returns>
        public static Dictionary<T, object> MergeDictionaries(Dictionary<T, object>[] dicts)
        {
            var result = dicts.SelectMany(dict => dict)
                         .ToDictionary(pair => pair.Key, pair => pair.Value);
            return result;
        }

        /// <summary>
        /// 将字典转换为实体对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dic"></param>
        /// <returns></returns>
        public static T ConvertDic<T>(Dictionary<string, object> dic)
        {
            T model = Activator.CreateInstance<T>();
            PropertyInfo[] modelPro = model.GetType().GetProperties();
            if (modelPro.Length > 0 && dic.Count() > 0)
            {
                for (int i = 0; i < modelPro.Length; i++)
                {
                    if (dic.ContainsKey(modelPro[i].Name))
                    {
                        modelPro[i].SetValue(model, dic[modelPro[i].Name], null);
                    }
                }
            }
            return model;
        }
    }
}
